package com.gxa.saas.controller;

import com.gxa.saas.pojo.Goods;
import com.gxa.saas.pojo.Storage;
import com.gxa.saas.service.GoodsService;
import com.gxa.saas.service.StorageService;
import com.gxa.saas.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author CZJ
 * @date 2022/6/7 14:36
 */
@Api(value = "商品列表相关接口")
@RestController
@RequestMapping("/goods")
@CrossOrigin
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @Autowired
    StorageService storageService;

    private JsonResult jsonResult=new JsonResult();


    @ApiOperation(value = "分页查询商品接口",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "查询页数",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页查询数",dataType = "Integer",paramType = "query")
    })
    @PostMapping("/selectGoodsByPage")
    /**
     * 分页查询商品接口
     */
    public Map getBrandByPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size){
        List<Goods> goods1 = goodsService.selectAll();
        List<Goods> goods = goodsService.selectByPage(page, size);
        jsonResult.set("number",goods1.size());
        jsonResult.setData(goods);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "获取商品列表接口",produces = "query")
    @PostMapping("/select")
    /**
     * 获取商品列表接口
     */
    public Map getBrandByPage(){
        List<Goods> goods = goodsService.selectAll();
        jsonResult.setData(goods);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "添加商品接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods",value = "添加的对象",dataType = "Goods",paramType = "body"),
            @ApiImplicitParam(name = "price1",value = "大单位成本价",dataType = "integer",paramType = "query"),
            @ApiImplicitParam(name = "sprice1",value = "小单位成本价",dataType = "integer",paramType = "query"),
            @ApiImplicitParam(name = "price2",value = "大单位供货价",dataType = "integer",paramType = "query"),
            @ApiImplicitParam(name = "sprice2",value = "小单位供货价",dataType = "integer",paramType = "query")
    })
    @PostMapping("/addGoods")
    /**
     * 添加商品接口
     */
    public Map addBrandSecond(@RequestBody Goods goods,Double price1,Double sprice1,
                              Double price2,Double sprice2){
        Integer insert = goodsService.insert(goods);
        Storage storage =new Storage();
        storage.setName(goods.getName());
        storage.setBigPrice(price1);
        storage.setBigUnit(goods.getBigUnit());
        storage.setSmallPrice(sprice1);
        storage.setSmallUnit(goods.getSmallUnit());
        storageService.addStorage(storage);
        jsonResult.setCode("200");
        jsonResult.setData(insert);
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "根据id删除商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "需要删除的id",dataType = "Integer",paramType = "query")

    })
    @PostMapping("/deleteGoods")
    /**
     * 根据id删除
     */
    public Map deleteBrand(Integer id){
        Integer delete = goodsService.delete(id);
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        jsonResult.setData(delete);
        return jsonResult.getMap();
    }


    @ApiOperation(value = "高级检索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "商品类型",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "brand",value = "商品品牌",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "factory",value = "生产厂商",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "isUsed",value = "是否上架(已上架/未上架),不传值就默认已上架",dataType = "string",paramType = "query")

    })
    @PostMapping("/selectGoodsHigh")
    /**
     * 高级检索
     */
    public Map High(String type,String brand,String factory,String isUsed){
        Boolean boo = true;
        if ("未上架".equals(isUsed)){
            boo=false;
        }
        Goods goods =new Goods();
        goods.setGoodsType(type);
        goods.setBrandName(brand);
        goods.setFactory(factory);
        goods.setIsUsed(boo);
        List<Goods> goods1 = goodsService.selectGoodsHigh(goods);
        jsonResult.setData(goods1);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "低级检索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "商品名称",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "type",value = "商品类型",dataType = "string",paramType = "query"),
            @ApiImplicitParam(name = "brand",value = "商品品牌",dataType = "string",paramType = "query")

    })
    @PostMapping("/selectGoodsLow")
    /**
     * 低级检索
     */
    public Map Low(String name,String type,String brand){
        Goods goods = new Goods();
        goods.setName(name);
        goods.setGoodsType(type);
        goods.setBrandName(brand);
        List<Goods> goods1 = goodsService.selectGoodsLow(goods);
        jsonResult.setData(goods1);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "修改上下架状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods",value = "修改的对象",dataType = "Goods",paramType = "body")

    })
    @PostMapping("/updateState")
    /**
     * 修改上下架状态
     * @param goods
     * @return
     */
    public Map updateState(@RequestBody Goods goods){
        Boolean isUsed = !goods.getIsUsed();
        goods.setIsUsed(isUsed);
        Integer update = goodsService.update(goods);
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        jsonResult.setData(update);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "修改排序方式,不传参就是升序")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sort",value = "修改的排序方式(升序排列/降序排列)",dataType = "string",paramType = "query")

    })
    @PostMapping("/changeSort")
    /**
     * 修改排序方式
     * @param goods
     * @return
     */
    public Map changeSort(String sort){
        Boolean boo = true;
        if ("降序排列".equals(sort)){
            boo=false;
        }
        List<Goods> goods = goodsService.selectBySort(boo);
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        jsonResult.setData(goods);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "全部导出")
    @PostMapping("/exportAll")
    public Map export(HttpServletResponse response) throws Exception{
        List<Goods> goods = goodsService.selectAll();
        goodsService.export(goods,response);
        jsonResult.setCode("200");
        jsonResult.setMsg("导出成功");
        return jsonResult.getMap();
    }


    @ApiOperation(value = "编辑商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goods",value = "修改的对象",dataType = "Goods",paramType = "body")

    })
    @PostMapping("/update")
    public Map update(@RequestBody Goods goods){
        Integer update = goodsService.update(goods);
        jsonResult.setMsg("修改成功");
        jsonResult.setCode("200");
        jsonResult.setData(update);
        return jsonResult.getMap();
    }

    @ApiOperation(value = "两个下拉框所需求的内容")
    @PostMapping("/selectTwo")
    public Map selectTwo(){
        List<Goods> goods = goodsService.selectTwo();
        jsonResult.setData(goods);
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        return jsonResult.getMap();
    }


}
